package me.buick.util.jmeter.snmpprocessvisualizers;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.tree.DefaultMutableTreeNode;

import org.apache.jmeter.samplers.Clearable;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jmeter.visualizers.MonitorListener;
import org.apache.jmeter.visualizers.MonitorModel;

public class SNMPProcessMonitorSNMPPanel extends JPanel implements MonitorListener, Clearable {

	private JPanel GRAPHPANEL;

	private SNMPProcessMonitorSNMPGraph GRAPH;

	private DefaultMutableTreeNode ROOTNODE;

	private SNMPProcessMonitorSNMPAccumModel MODEL;

	private SampleResult ROOTSAMPLE;

	public static final String LEGEND_CPUUSAGE = "Cpu Usage"; //$NON-NLS-1$
	public static final String LEGEND_MEM = "Memory";

	public static final ImageIcon LEGEND_CPU_ICON = JMeterUtils.getImage("monitor-green-legend.gif"); //$NON-NLS-1$
	public static final ImageIcon LEGEND_MEM_ICON = JMeterUtils.getImage("monitor-blue-legend.gif"); //$NON-NLS-1$

	public static final String GRID_LABEL_TOP = JMeterUtils.getResString("monitor_label_left_top"); //$NON-NLS-1$

	public static final String GRID_LABEL_MIDDLE = JMeterUtils.getResString("monitor_label_left_middle"); //$NON-NLS-1$

	public static final String GRID_LABEL_BOTTOM = JMeterUtils.getResString("monitor_label_left_bottom"); //$NON-NLS-1$

	public static final String GRID_LABEL_HEALTHY = JMeterUtils.getResString("monitor_label_right_healthy"); //$NON-NLS-1$

	public static final String GRID_LABEL_DEAD = JMeterUtils.getResString("monitor_label_right_dead"); //$NON-NLS-1$

	public static final String PERF_TITLE = JMeterUtils.getResString("monitor_performance_title"); //$NON-NLS-1$

	public static final String SERVER_TITLE = JMeterUtils.getResString("monitor_performance_servers"); //$NON-NLS-1$

	protected Font plaintext = new Font("plain", Font.TRUETYPE_FONT, 10); //$NON-NLS-1$

	/**
	 * 
	 * @deprecated Only for use in unit testing
	 */
	public SNMPProcessMonitorSNMPPanel() {
		// log.warn("Only for use in unit testing");
	}

	/**
	 * 
	 */
	public SNMPProcessMonitorSNMPPanel(SNMPProcessMonitorSNMPAccumModel model, SNMPProcessMonitorSNMPGraph graph) {
		super();
		this.MODEL = model;
		this.MODEL.addListener(this);
		this.GRAPH = graph;
		init();
	}

	/**
	 * init() will create all the necessary swing panels, labels and icons for
	 * the performance panel.
	 */
	private void init() {// called from ctor, so must not be overridable

		this.setLayout(new BorderLayout());
		JLabel title = new JLabel(" " + PERF_TITLE);
		title.setPreferredSize(new Dimension(200, 40));
		GRAPHPANEL = new JPanel();
		GRAPHPANEL.setLayout(new BorderLayout());
		GRAPHPANEL.setMaximumSize(new Dimension(MODEL.getBufferSize(), MODEL.getBufferSize()));
		GRAPHPANEL.setBackground(Color.white);
		GRAPHPANEL.add(GRAPH, BorderLayout.CENTER);
		this.add(GRAPHPANEL, BorderLayout.CENTER);

		this.add(title, BorderLayout.NORTH);
		this.add(createLegend(), BorderLayout.SOUTH);
		this.add(createLeftGridLabels(), BorderLayout.WEST);
		this.add(createRightGridLabels(), BorderLayout.EAST);
	}

	/**
	 * Method will create the legends at the bottom of the performance tab
	 * explaining the meaning of each line.
	 * 
	 * @return JPanel
	 */
	public JPanel createLegend() {
		Dimension lsize = new Dimension(130, 18);

		JPanel legend = new JPanel();
		legend.setLayout(new FlowLayout());
		
		JLabel load = new JLabel(LEGEND_CPUUSAGE);
		load.setFont(plaintext);
		load.setPreferredSize(lsize);
		load.setIcon(LEGEND_CPU_ICON);
		legend.add(load);
		
		JLabel load2 = new JLabel(LEGEND_MEM);
		load2.setFont(plaintext);
		load2.setPreferredSize(lsize);
		load2.setIcon(LEGEND_MEM_ICON);
		legend.add(load2);

		return legend;
	}

	/**
	 * Method is responsible for creating the left grid labels.
	 * 
	 * @return JPanel
	 */
	public JPanel createLeftGridLabels() {
		Dimension lsize = new Dimension(33, 20);
		JPanel labels = new JPanel();
		labels.setLayout(new BorderLayout());

		JLabel top = new JLabel(" " + GRID_LABEL_TOP);
		top.setFont(plaintext);
		top.setPreferredSize(lsize);
		labels.add(top, BorderLayout.NORTH);

		JLabel mid = new JLabel(" " + GRID_LABEL_MIDDLE);
		mid.setFont(plaintext);
		mid.setPreferredSize(lsize);
		labels.add(mid, BorderLayout.CENTER);

		JLabel bottom = new JLabel(" " + GRID_LABEL_BOTTOM);
		bottom.setFont(plaintext);
		bottom.setPreferredSize(lsize);
		labels.add(bottom, BorderLayout.SOUTH);
		return labels;
	}

	/**
	 * Method is responsible for creating the grid labels on the right for
	 * "healthy" and "dead"
	 * 
	 * @return JPanel
	 */
	public JPanel createRightGridLabels() {
		JPanel labels = new JPanel();
		labels.setLayout(new BorderLayout());
		labels.setPreferredSize(new Dimension(40, GRAPHPANEL.getWidth() - 100));
		Dimension lsize = new Dimension(40, 20);
		JLabel h = new JLabel(GRID_LABEL_HEALTHY);
		h.setFont(plaintext);
		h.setPreferredSize(lsize);
		labels.add(h, BorderLayout.SOUTH);

		JLabel d = new JLabel(GRID_LABEL_DEAD);
		d.setFont(plaintext);
		d.setPreferredSize(lsize);
		labels.add(d, BorderLayout.NORTH);
		return labels;
	}

	
	public synchronized void addSample(MonitorModel model) {
		GRAPH.updateGui(model);
	}


	/**
	 * clear will remove all child nodes from the ROOTNODE, clear the HashMap,
	 * update the graph and jpanel for the server tree.
	 */
	public void clearData() {
		ROOTNODE.removeAllChildren();
		GRAPH.clearData();
	}
}